############ Slovak deputies discourse networks: War in Ukraine ############

# rename the excel file first - no spaces or dashes
getwd()
setwd("C:/Users/tomas/OneDrive - Filozofická fakulta, 
      Univerzita Karlova/práce/projekty/SVKposlanci_DNA")

# data loading and preparation
library(openxlsx)
library(tidyverse)

dat <- read.xlsx("Slovak MPs DNA data.xlsx", colNames = T, sheet = 1)

subDat <- dat[c(1:117), c(1, 3, 5, 6)]

subDat2 <- dat[c(118:237), c(1, 9)]

table(subDat$party)
table(subDat$gender)
summary(subDat$followers)
table(subDat2$category)

# preparing dummy variables
library(fastDummies)

subDat <- dummy_cols(subDat, select_columns = c('party', 'gender'))
view(subDat)

attrMPs <- subset(subDat, select = -c(party_no_party, party_PS, party_SPOLU, gender_m))
view(attrMPs)

subDat2 <- dummy_cols(subDat2, select_columns = 'category')
view(subDat2)

attrTopics <- subset(subDat2, select = -c(category_misc, category_pro_President))

# preparing incidence matrices
mat1 <- read.xlsx("Slovak MPs DNA data.xlsx", colNames = T, rowNames = T, sheet = 2)
mat2 <- read.xlsx("Slovak MPs DNA data.xlsx", colNames = T, rowNames = T, sheet = 3)

topicsPre <- t(mat1)
topicsPost <- t(mat2)

# dichotomise the matrices
matPre <- ifelse(topicsPre >= 1, 1, 0)
matPost <- ifelse(topicsPost >= 1, 1, 0)

# dyadic covariate - shared committees
committees <- as.matrix(read.xlsx("committee matrix.xlsx", colNames = T, 
                                  rowNames = T, sheet = 1))

co_com <- committees %*% t(committees)

comDeg <- rowSums(committees)

# descriptive analysis
library(migraph)

autographr(topicsPre, node_color = "type")

is_migraph(matPre)
is_twomode(matPre)

graph_density(matPre)
graph_density(matPost)

graph_degree(matPre)
graph_degree(matPost)

graph_equivalency(matPre)
graph_equivalency(matPost)

head(sort(rowSums(matPre), decreasing = T), n = 10)
head(sort(rowSums(matPost), decreasing = T), n = 10)

head(sort(colSums(matPre), decreasing = T), n = 10)
head(sort(colSums(matPost), decreasing = T), n = 10)

head(sort(node_degree(matPre), decreasing = T), n = 10)
node_degree(topicsPost)
node_betweenness()

# stochastic actor-oriented model
library(RSiena)

nrMPs <- dim(topicsPre)[1]
nrTopics <- dim(topicsPre)[2]

# define different node sets
MPs <- sienaNodeSet(nrMPs, nodeSetName = "MPs")
topics <- sienaNodeSet(nrTopics, nodeSetName = "Topics")

# bipartite dependent network
TopicsPrePost <- sienaDependent(array(c(matPre, matPost),
                              dim = c(nrMPs, nrTopics, 2)),
                        "bipartite", nodeSet = c("MPs", "Topics"))

# include covariates
gender <- coCovar(attrMPs$gender_f, nodeSet = "MPs")
followers <- coCovar(attrMPs$followers, nodeSet = "MPs")
HLAS <- coCovar(attrMPs$party_HLAS, nodeSet = "MPs")
LSNS <- coCovar(attrMPs$party_LSNS, nodeSet = "MPs")
OLaNO <- coCovar(attrMPs$party_OLaNO, nodeSet = "MPs")
Republika <- coCovar(attrMPs$party_Republika, nodeSet = "MPs")
SaS <- coCovar(attrMPs$party_SaS, nodeSet = "MPs")
SMER <- coCovar(attrMPs$party_SMER, nodeSet = "MPs")
SMERODINA <- coCovar(attrMPs$party_SMERODINA, nodeSet = "MPs")
ZaLudi <- coCovar(attrMPs$party_ZaLudi, nodeSet = "MPs")
ZIVOT <- coCovar(attrMPs$party_ZIVOT, nodeSet = "MPs")
nrComms <- coCovar(comDeg, nodeSet = "MPs")

antiChProsec <- coCovar(attrTopics$category_anti_Chief_Prosecutor, nodeSet = "Topics")
antiCoal <- coCovar(attrTopics$category_anti_Coallition, nodeSet = "Topics")
antiNATO <- coCovar(attrTopics$category_anti_NATO, nodeSet = "Topics")
antiOpp <- coCovar(attrTopics$category_anti_Opposition, nodeSet = "Topics")
antiPres <- coCovar(attrTopics$category_anti_President, nodeSet = "Topics")
proChProsec <- coCovar(attrTopics$category_pro_Chief_Prosecutor, nodeSet = "Topics")
proNATO <- coCovar(attrTopics$category_pro_NATO, nodeSet = "Topics")
proRussia <- coCovar(attrTopics$category_pro_Russia, nodeSet = "Topics")
proUkraine <- coCovar(attrTopics$category_pro_Ukraine, nodeSet = "Topics")
SvkNeutral <- coCovar(attrTopics$category_Slovak_neutrality, nodeSet = "Topics")

# combine data for the analysis:
bipData <- sienaDataCreate(gender, followers, HLAS, LSNS, OLaNO, Republika, SaS,
                           SMER, SMERODINA, ZaLudi, ZIVOT, nrComms,
                           antiChProsec, antiCoal, antiNATO, antiOpp, antiPres,
                           proChProsec, proNATO, proRussia, proUkraine, SvkNeutral,
                           TopicsPrePost,
                           nodeSets = list(MPs, topics))

# get effects table for model specification
bipEffects <- getEffects(bipData)
print01Report(bipData, modelname = "SVK_MPs_Topics")

# create algorithm object
bipAlg <- sienaAlgorithmCreate(projname = 'SVK_MPs_Topics', cond = F)

# specify the model
bipEffects <- includeEffects(bipEffects, cycle4, name = "TopicsPrePost")
bipEffects <- includeEffects(bipEffects, outActSqrt, name = "TopicsPrePost")
bipEffects <- includeEffects(bipEffects, inPopSqrt, name = "TopicsPrePost")
bipEffects <- includeEffects(bipEffects, outIso, name = "TopicsPrePost")
#bipEffects <- includeEffects(bipEffects, inIso, name = "TopicsPrePost")
bipEffects <- includeEffects(bipEffects, outRateLog, type = "rate", fix = T, 
                             test = T, include = T)

bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "gender")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "HLAS")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "LSNS")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "OLaNO")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "Republika")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "SaS")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "SMER")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "SMERODINA")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "ZaLudi")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "ZIVOT")
bipEffects <- includeEffects(bipEffects, simEgoInDist2, name = "TopicsPrePost", 
                             interaction1 = "followers")

bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "gender")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "HLAS")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "LSNS")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "OLaNO")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "Republika")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "SaS")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "SMER")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "SMERODINA")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "ZaLudi")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "ZIVOT")
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "followers", include = F)
bipEffects <- includeEffects(bipEffects, egoX, name = "TopicsPrePost", 
                             interaction1 = "nrComms")

bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "antiChProsec")
bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "antiCoal")
bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "antiNATO")
bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "antiOpp")
bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "antiPres")
bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "proChProsec")
bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "proNATO")
bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "proRussia")
bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "proUkraine")
bipEffects <- includeEffects(bipEffects, altX, name = "TopicsPrePost",
                             interaction1 = "SvkNeutral")

#bipEffects <- includeEffects(bipEffects, sameXCycle4, name = "music", interaction1 = "sex")
bipEffects

# estimate the model
bipResults <- siena07(bipAlg, data = bipData, effects = bipEffects, returnDeps = T,
                      useCluster = TRUE, nbrNodes = 6) #prevAns = bipResults
bipResults

# goodness of fit check 
gofID <- sienaGOF(bipResults, verbose = TRUE, varName = "TopicsPrePost", 
                  IndegreeDistribution)
gofID
plot(gofID)

gofOD <- sienaGOF(bipResults, verbose = TRUE, varName = "TopicsPrePost", 
                  OutdegreeDistribution)
gofOD
plot(gofOD)

# alternative run
bipAlg1 <- sienaAlgorithmCreate(projname = 'SVK_MPs_Topics', cond = F, nsub = 1, 
                               n3 = 3000)
bipResults1 <- siena07(bipAlg1, data = bipData, effects = bipEffects, returnDeps = T,
                      useCluster = TRUE, nbrNodes = 6, prevAns = bipResults)
bipResults1

# goodness of fit check 
gofID1 <- sienaGOF(bipResults1, verbose = TRUE, varName = "TopicsPrePost", 
                  IndegreeDistribution)
gofID1
plot(gofID1)

gofOD1 <- sienaGOF(bipResults1, verbose = TRUE, varName = "TopicsPrePost", 
                  OutdegreeDistribution)
gofOD1
plot(gofOD1)
